﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace OALPTool
{
    public partial class DataGridViewDemoMain : Form
    {

        #region 自定义变量 

        /// <summary>
        /// DataGridView数据源
        /// </summary>
        private DataView dv = new DataView();
        /// <summary>
        /// 冻结列数
        /// </summary>
        private int nupindex = 0;

        #endregion

        #region 界面初始化

        public DataGridViewDemoMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化界面
        /// </summary>
        /// <returns></returns>
        public int InitForm() 
        {
            this.InitTable();

            this.SetDgvStyle();

            return 0;
        }

        #endregion

        #region 数据源初始化

        /// <summary>
        /// 初始化DataGridView数据源
        /// </summary>
        public void InitTable() 
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("corpname", typeof(String));
            dt.Columns.Add("departname", typeof(String));
            dt.Columns.Add("year", typeof(String));
            dt.Columns.Add("month", typeof(String));
            dt.Columns.Add("userno", typeof(String));
            dt.Columns.Add("username", typeof(String));
            dt.Columns.Add("sex", typeof(String));
            dt.Columns.Add("amt",typeof(Double));
            dt.Columns.Add("flag", typeof(String));

            DataRow dr = dt.NewRow();
            dr["corpname"] = "江南发电集团";
            dr["departname"] = "总经理办公室";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "1000001";
            dr["username"] = "徐予华";
            dr["sex"] = "1";
            dr["amt"] = 1000; 
            dr["flag"] = "是";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["corpname"] = "江南发电集团";
            dr["departname"] = "总经理办公室";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "1000002";
            dr["username"] = "陈立文";
            dr["sex"] = "0";
            dr["amt"] = 1000; 
            dr["flag"] = "否";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["corpname"] = "江南发电集团";
            dr["departname"] = "财务部";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "1000002";
            dr["username"] = "蔡宁";
            dr["sex"] = "1";
            dr["amt"] = 800; 
            dr["flag"] = "是";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["corpname"] = "江南发电集团";
            dr["departname"] = "财务部";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "1000002";
            dr["username"] = "丁则然";
            dr["sex"] = "0";
            dr["amt"] = 800; 
            dr["flag"] = "是";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["corpname"] = "江南发电集团";
            dr["departname"] = "财务部";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "1000002";
            dr["username"] = "顾建国";
            dr["sex"] = "0";
            dr["amt"] = 800; 
            dr["flag"] = "是";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["corpname"] = "江南发电集团";
            dr["departname"] = "财务部";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "1000002";
            dr["username"] = "王晓琴";
            dr["sex"] = "1";
            dr["amt"] = 800; 
            dr["flag"] = "否";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["corpname"] = "江南发电集团";
            dr["departname"] = "财务部";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "1000002";
            dr["username"] = "傅伯文";
            dr["sex"] = "1";
            dr["amt"] = 800; 
            dr["flag"] = "是";
            dt.Rows.Add(dr);

            dt.AcceptChanges();

            this.dv = dt.DefaultView;

        }

        #endregion

        #region DataGridView操作

        /// <summary>
        /// 设置DataGridView样式
        /// </summary>
        private void SetDgvStyle() 
        {
            DataGridViewFunc.setDgvStyle(this.getColList(), ref this.dgv, this.dv);
            
            this.dgv.AutoGenerateColumns = false;
            this.dgv.AllowUserToAddRows = false;
            this.dgv.AllowUserToDeleteRows = false;

            this.dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //不可排序
            foreach (DataGridViewColumn dgCol in this.dgv.Columns)
            {
                dgCol.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }

        /// <summary>
        /// 获取DataGridView样式列表
        /// </summary>
        /// <returns>返回样式列表</returns>
        private ArrayList getColList() 
        {
            ///ComboBox数据源，object[0]为Display值，object[1]为Value值
            ArrayList sexList = new ArrayList();
            sexList.Add(new object[] { "男", "0" });
            sexList.Add(new object[] { "女", "1" });

            ArrayList colList = new ArrayList();
            ///   0:字段中文名；
            ///   1:字段英文名；
            ///   2:值类型【0-TextBox(默认),1-CheckBox,2-ComboBox,3-DateTimePicker,6-TextBox(自动换行),7-超链接】；
            ///   3:显示宽度；
            ///   4:显示格式化【"c"表示数字格式化；"d"表示时间格式化】；
            ///   5:对齐方式；
            ///   6:是否只读【true为只读；false为可修改】；
            ///   7:数据为空时显示；
            ///   8:是否存在转义；
            ///   9:显示顺序号；
            ///   10:颜色字段名【暂时未用】；
            ///   11:颜色字段意义列表【暂时未用】；
            ///   12:格式化字段；
            ///   13:暂时没用；
            ///   14:如果值类型为2，那么该值为Combol项集合对应数据集【每条数据对应一个object集合，object[0]为Display值，object[1]为Value值】            
            
            colList.Add(new object[] { "单位", "corpname", "0", 180, null, DataGridViewContentAlignment.MiddleCenter, true, "", null, 1, "", "", "", "", "" });
            colList.Add(new object[] { "部门", "departname", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, true, "", null, 2, "", "", "", "", "" });
            colList.Add(new object[] { "工号", "userno", "0", 80, null, DataGridViewContentAlignment.MiddleCenter, true, "", null, 3, "", "", "", "", "" });
            colList.Add(new object[] { "姓名", "username", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 4, "", "", "", "", "" });
            colList.Add(new object[] { "性别", "sex", "2", 80, null, DataGridViewContentAlignment.MiddleCenter, true, "", null, 5, "", "", "", "", sexList });
            colList.Add(new object[] { "津贴", "amt", "0", 100, "c", DataGridViewContentAlignment.MiddleRight, true, "", null, 6, "", "", "#,##0.00", "", "" });
            colList.Add(new object[] { "在职状态", "flag", "1", 100, null, DataGridViewContentAlignment.MiddleCenter, true, "", null, 6, "", "", "", "", "" });

            return colList;
        }

        /// <summary>
        /// DataGridView列设置
        /// </summary>
        private void tbbSetStyle_Click(object sender, EventArgs e)
        {
            ArrayList args = new ArrayList();
            args.Add(this.dgv);
            args.Add(this.getColList());
            args.Add(this.dv);
            args.Add(this.nupindex);
            args.Add("员工档案列设置");
            int ret = 0;
            int sysret = TSysPublic.ExecFunction("SAL_FrmColStyleSetRefresh", ref args, out ret);
            if (sysret != 0)
            {
                TSysPublic.ShowErrMsg("调用【SAL_FrmColStyleSetRefresh】出错，错误码为：" + sysret.ToString());
                return;
            }
            if (ret < 0)
            {
                TSysPublic.ShowErrMsg("执行【SAL_FrmColStyleSetRefresh】出错，错误码为：" + ret.ToString());
                return;
            }
            else if (ret == 1)
            {
                this.dgv = (DataGridView)args[0];
            }
        }

        /// <summary>
        /// DataGridView导出到Excel
        /// </summary>
        private void tbbExcel_Click(object sender, EventArgs e)
        {
            ArrayList titleList = new ArrayList();
            //Excel标题
            DataTable dtTitle = new DataTable();
            dtTitle.Columns.Add("BegRow", typeof(int));
            dtTitle.Columns.Add("EndRow", typeof(int));
            dtTitle.Columns.Add("BegCol", typeof(int));
            dtTitle.Columns.Add("EndCol", typeof(int));
            dtTitle.Columns.Add("Text", typeof(string));
            dtTitle.Columns.Add("Align", typeof(string));

            ArrayList titlelt1 = new ArrayList();

            DataRow drTitleNew = dtTitle.NewRow();
            drTitleNew["BegRow"] = 1;
            drTitleNew["EndRow"] = 1;
            drTitleNew["BegCol"] = 1;
            drTitleNew["EndCol"] = this.dgv.ColumnCount;
            drTitleNew["Text"] = "员工津贴";
            drTitleNew["Align"] = "0";
            titlelt1.Add(drTitleNew);
            titleList.Add(titlelt1);

            ArrayList titlelt2 = new ArrayList();
            DataRow drTitleNew1 = dtTitle.NewRow();
            drTitleNew1["BegRow"] = 2;
            drTitleNew1["EndRow"] = 2;
            drTitleNew1["BegCol"] = 1;
            drTitleNew1["EndCol"] = this.dgv.ColumnCount;
            drTitleNew1["Text"] = "导出日期: " + System.DateTime.Now.ToShortDateString() + "    ";
            drTitleNew1["Align"] = "2";
            titlelt2.Add(drTitleNew1);

            titleList.Add(titlelt2);

            ArrayList Args = new ArrayList();
            object objs = null;
            int ret = 0;
            Args.Add(this.dgv);
            Args.Add(titleList);
            ret = TSysPublic.ExecFunction("SAL_DataGridViewToExcel", ref Args, out objs);
            if (ret < 0)
            {
                TSysPublic.ShowMsg("导出到Excel失败!");
            }
        }

        /// <summary>
        /// 冻结列
        /// </summary>
        private void refreshFrozenColumn()
        {
            int index = 0;
            try
            {
                index = Convert.ToInt32(this.numericUpDown1.Value);
            }
            catch (System.OverflowException ofe)
            {
                MessageBox.Show(ofe.Message);
            }

            int colCount = 0;

            for (int i = 0; i < this.dgv.ColumnCount; i++)
            {
                if (this.dgv.Columns[i].Visible == true)
                {
                    colCount++;
                }
            }

            if (index < 0)
            {
                return;
            }
            if (index > colCount)
            {
                TSysPublic.ShowErrMsg("输入的列数超过总列数!");
                return;
            }

            ArrayList args = new ArrayList();
            args.Add(this.dgv);
            args.Add(index);
            int ret = 0;
            int sysret = TSysPublic.ExecFunction("SAL_doFrozenColumn", ref args, out ret);
            if (sysret != 0)
            {
                TSysPublic.ShowErrMsg("调用【SAL_doFrozenColumn】出错，错误码为：" + sysret.ToString());
                return;
            }
            if (ret < 0)
            {
                TSysPublic.ShowErrMsg("执行【SAL_doFrozenColumn】出错，错误码为：" + ret.ToString());
                return;
            }
            else if (ret == 1)
            {
                this.dgv = (DataGridView)args[0];
            }

            this.nupindex = index;

        }

        /// <summary>
        /// 重绘DataGridView  显示序号
        /// </summary>
        private void dgv_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //显示DataGridView序号
            DataGridViewFunc.disDgvSerialno(this.dgv, e);
        }

        #endregion

        #region 界面事件

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            this.refreshFrozenColumn();
        }

        private void tbbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

    }
}
